from django.shortcuts import render
from django.views import View

from .models import Content, ContentCategory
from .utils import get_categories


class IndexView(View):
    """首页"""
    def get(self, request):

        # 定义字典用来包装所有广告数据
        contents = {}
        # 查询所有广告类别数据
        content_cat_qs = ContentCategory.objects.all()
        # 遍历广告类型查询集,依次包装所有广告数据
        for content_cat_model in content_cat_qs:
            contents[content_cat_model.key] = content_cat_model.content_set.filter(status=True).order_by('sequence')


        context = {
            'categories': get_categories(),  # 商品类别所有数据
            'contents': contents  # 所有广告数据
        }
        return render(request, 'index.html', context)